<?php
!defined('P_W') && exit('Forbidden');
$houseBaseUrl = "index.php?m=$m&";
require_once R_P.'mode/house/lib/groupbuy.class.php';
include_once R_P.'data/bbscache/house_config.php';
$pw_groupbugDB = House::loadDB('groupbuy');
$pw_groupbug = new PW_Groupbuy();
InitGP(array('budget','area','hid'),'GP');

$budgetHtml = '<select name="budget"><option value=""></option>';
foreach ($house_field['budget'] as $key => $value) {
	$budget && $_selected = $key == $budget ? 'selected' : '';
	$budgetHtml .= '<option value="'.$key.'" '.$_selected.'>'.$value['value'].'</option>';
}
$budgetHtml .= '</select>';
$areaHtml = '<select name="area"><option value=""></option>';
$_selected = '';
foreach ($house_field['acreage'] as $key => $value) {
	$area && $_selected = $key == $area ? 'selected' : '';
	$areaHtml .= '<option value="'.$key.'" '.$_selected.'>'.$value['value'].'</option>';
}
$areaHtml .= '</select>';
if ($hid) {
	$_houseinfo = House::LoadClass('HouseInfo');
	$houseinfo  = $_houseinfo->getAllInfoById($hid);
}
if (empty($action) || $action == 'search') {
	InitGP(array('page'),'GP',2);
	InitGP(array('hname','gender','register','stime','etime'),'GP',1);
	$searchurl = $basename.'&action=search';
	!empty($hid) && $searchurl .= '&hid='.$hid;
	!empty($budget) && $searchurl .= '&budget='.$budget;
	!empty($area) && $searchurl .= '&area='.$area;
	!empty($hname) && $searchurl .= '&hname='.$hname;
	!empty($gender) && $searchurl .= '&gender='.$gender;
	!empty($register) && $searchurl .= '&register='.$register;
	!empty($stime) && $searchurl .= '&stime='.$stime;
	!empty($etime) && $searchurl .= '&etime='.$etime; 
	!$page && $page = 1;
	$gender_W = $gender_M = $register_Y = $register_N = '';
	if ($gender == '1') {
		$gender_W = 'selected';
	} elseif ($gender == '2') {
		$gender_M = 'selected';
	} else {
		$gender_W = $gender_M = '';
	}
	if ($register == 'ne') {
		$register_Y = 'selected';
		$register_N = '';
	} elseif ($register == 'eq') {
		$register_Y = '';
		$register_N = 'selected';
	} else {
		$register_1 = $register_0 = '';
	}
	$stime && $_stime = PwStrtoTime($stime);
	$etime && $_etime = PwStrtoTime($etime);
	if (!empty($houseinfo)) {
		$hname = $houseinfo['name'];
		$_readonly = "readonly";
	}
	$conditions = array(array($hid,'eq','hid'),
						array($hname,'like','hname'),
						array($gender,'eq','gender'),
						array($budget,'eq','budget'),
						array($area,'eq','area'),
						array('0',$register,'uid'),
						array($_stime,'te','ctime'),
					    array($_etime,'le','ctime'));
	$pw_groupbugDB->setDb_perpage(10);
	$pw_groupbugDB->setBasename($searchurl);
	$groupbys = $pw_groupbugDB->queryHouseGroupbuy($conditions,$page, true);
	$totalCount = $groupbys['totalCount'] ? $groupbys['totalCount'] : 0;
	$groupbyAll = $pw_groupbugDB->queryHouseGroupbuy($conditions);
	$allIds = "";
	foreach ($groupbyAll['contents'] as $key => $value) {
		if ($value['uid'] != '0' && stripos(','.$allIds.',' , ','.$value['uid'].',') === false) {
			$allIds .= $allIds ? ','.$value['uid'] : $value['uid'];
		}
	}
} elseif ($action == 'del') {
	InitGP(array('id'),'GP',1);
	if (empty($id)){
		adminmsg('operate_error');
	}
	$pw_groupbugDB->deleteHouseGroupbuy($id);
	adminmsg('operate_success');
} elseif ($action == 'sendmsg') {
	InitGP(array('step','nexto'));
	if (empty($step) || $step == 1) {
		InitGP(array('id','allIds'));
		if (empty($id) || empty($allIds)) {
			adminmsg('operate_error');
		}
		$uids = '';
		if (empty($id)) {
			$uids = $allIds;
		}else{
			$result = $pw_groupbugDB->queryHouseGroupbuyByIds($id);
			foreach ($result as $key => $value) {
				if ($value['uid'] != '0' && stripos(','.$uids.',' , ','.$value['uid'].',') === false) {
					$uids .= $uids ? ','.$value['uid'] : $value['uid'];
				}
			}
		}
		include PrintMode('groupbuy');
		exit;
	}elseif($step == 2){
		InitGP(array('subject','atc_content','uids'));
		$cache_file = D_P."data/bbscache/".substr(md5($admin_pwd),10,10).".txt";
		if (!$nexto) {
			writeover($cache_file,$atc_content);
		} else {
			$atc_content = readover($cache_file);
		}
		if (empty($subject) || empty($atc_content)) {
			adminmsg('sendmsg_empty','javascript:history.go(-1);');
		}
		$subject     = Char_cv($subject);
		$sendmessage = Char_cv($atc_content);
		$percount = 1;
		empty($nexto) && $nexto = 1;
		$uids = explode(',',$uids);
		$count = count($uids);
		if ($uids) {
			$uids = pwImplode($uids);
			$msg_a = array();
			$query = $db->query("SELECT uid,username,email,newpm FROM pw_members WHERE uid IN($uids)");
			while (@extract($db->fetch_array($query))) {
				$sendmessage = str_replace("\$email",$email,$atc_content);
				$sendmessage = str_replace("\$windid",$username,$sendmessage);
				$msg_a[] = array($uid,'0','System','rebox','1',$timestamp,$subject,$sendmessage);
			}
			//TODO 新消息提醒
			if ($msg_a) {
				require_once(R_P.'require/msg.php');
				send_msgc($msg_a);
			}
		}
		$havesend = $nexto*$percount;
		if ($count > ($nexto*$percount)) {
			$nexto++;
			$j_url = "$basename&action=$action&step=2&nexto=$nexto&subject=".rawurlencode($subject);
			adminmsg("sendmsg_step",EncodeUrl($j_url),1);
		} else {
			P_unlink($cache_file);
			adminmsg('operate_success');
		}
	}
} elseif ($action == 'stats') {
	InitGP(array('hname','gender','register','stime','etime'),'GP',1);
	$searchurl = $basename.'&action=search';
	!empty($hid) && $searchurl .= '&hid='.$hid;
	!empty($budget) && $searchurl .= '&budget='.$budget;
	!empty($area) && $searchurl .= '&area='.$area;
	!empty($hname) && $searchurl .= '&hname='.$hname;
	!empty($gender) && $searchurl .= '&gender='.$gender;
	!empty($register) && $searchurl .= '&register='.$register;
	!empty($stime) && $searchurl .= '&stime='.$stime;
	!empty($etime) && $searchurl .= '&etime='.$etime; 
	$gender_W = $gender_M = $register_Y = $register_N = ''; 
	if ($gender == '1') {
		$gender_W = 'selected';
	} elseif ($gender == '2') {
		$gender_M = 'selected';
	} else {
		$gender_W = $gender_M = '';
	}
	if ($register == 'ne') {
		$register_Y = 'selected';
		$register_N = '';
	} elseif ($register == 'eq') {
		$register_Y = '';
		$register_N = 'selected';
	} else {
		$register_1 = $register_0 = '';
	}
	$stime && $_stime = PwStrtoTime($stime);
	$etime && $_etime = PwStrtoTime($etime);
	if (!empty($houseinfo)) {
		$hname = $houseinfo['name'];
		$_readonly = "readonly";
	}
	$conditions = array(array($hid,'eq','hid'),
						array($hname,'like','hname'),
						array($gender,'eq','gender'),
						array($budget,'eq','budget'),
						array($area,'eq','area'),
						array('0',$register,'uid'),
						array($_stime,'te','ctime'),
					    array($_etime,'le','ctime'));
	$groupbys = $pw_groupbugDB->queryHouseGroupbuy($conditions);
	$count = count($groupbys['contents']);
	$statsinfo = $pw_groupbug->statsHouseGroupbuy($groupbys['contents']);
} elseif ($action == 'detail'){
	define('AJAX',1);
	InitGP(array('id'),'GP',2);
	$result = $pw_groupbugDB->queryHouseGroupbuy($id);
	$userdetail = $result['contents'][0];
	include PrintMode('groupbuy');ajax_footer();
} elseif ($action == 'editStatsCount') {
	InitGP(array('statsCount'),'GP',1);
	!is_numeric($statsCount) && adminmsg('输入了错误的数据类型',"$basename&action=stats&hid=$hid");
	if (!$statsCount) {
		$statsCount = $pw_groupbugDB->queryHouseGroupbuyAllCount(array('hid'=>array($hid,'eq')));
	}
	$pw_groupbugDB->updateHouseGroupbuyCountByHid(array($hid),$statsCount);
	adminmsg('operate_success',"$basename&action=stats&hid=$hid");
}
include PrintMode('groupbuy');
exit;
?>